<?php
//测试控制器类
class ContentController extends Controller{
	
	public function __init(){
		parent::__init();
		header('Content-type: text/html;charset=utf-8');
	}

    //动作方法
    public function index(){
    	$gid = Q('get.gid',0,'intval');
		session('url',__ACTION__."gid/{$gid}");
		//获得当前商品信息和详情
		$data = M()->join("__goods__ g JOIN __goods_detail__ gd ON g.gid=gd.goods_gid JOIN __brands__ b ON g.brands_bid=b.bid JOIN __category__ c ON g.category_cid=c.cid")->where("g.gid={$gid}")->find();
//		p($data);die;

		// 把图片分成数组再压入
		$data['small_pic'] = explode(',', $data['small_pic']);
		$data['mid_pic'] = explode(',', $data['mid_pic']);
		$data['big_pic'] = explode(',', $data['big_pic']);
		
		//获得当前商品的属性和属性所对应的类型属性
		$attr = M()->join("__goods_attr__ ga JOIN __type_attr__ ta ON ga.type_attr_taid=ta.taid")->where("ga.goods_gid={$gid}&&ta.class=1")->all();
//		p($attr);
		// 循环数组，把功效和容量压入商品变量中
		foreach ($attr as $key => $value) {
			if ($value['taname']=='容量') {
				$data['capacity'][$key]['gaid'] = $value['gaid'];
				$data['capacity'][$key]['gavalue'] = $value['gavalue'];
			}
			if ($value['taname']=='功效') {
				$data['effect'][$key]['gaid'] = $value['gaid'];
				$data['effect'][$key]['gavalue'] = $value['gavalue'];
			}
		}
		// 分配变量
//		 p($data);
		$this->assign('data',$data);

        //显示视图
        $this->display();
    }
    
	// 尺码异步
	public function ajax_size(){
		// p(Q('post.'));die;
		// 获得当前商品gid,可以找到当前商品的货品列表
		$gid = Q('post.gid',0,'intval');
		// 组合id,把逗号去掉
		$group =ltrim(Q('post.group'),',');
		// 找到当前商品所对应的货品列表
		$listData = K('Goods_list')->where("goods_gid={$gid}")->all();
		// 定义一个变量
		$many =0;
		// 循环货品列表，如果组合id相同就得到他的数量
		foreach ($listData as $k => $v) {
			if ($v['group']==$group) {
				$many = $v['many'];
			}
		}
		// 获得规格所对应的添加金额
		$arr = explode(',', $group);
		$addmoney = 0;
		foreach ($arr as $k => $v) {
			$find = K('Goods_attr')->where("gaid={$v}")->find();
			$addmoney = $addmoney+$find['addmoney'];
		}
		$data = array(
			0=>$many,
			1=>$addmoney
			);
		// 反出库存数量
		$this->ajax($data);
	}
    
}
